import React from 'react';
import {Input,Row,Col,Button,message,Modal} from 'antd';
import $ from 'jquery';
import config from '../config/config';
import {browserHistory} from 'react-router';
const allRouter=config.router;
const token = sessionStorage.getItem('token');
class UpdatePassword extends React.Component {
    constructor(props){
        super(props);
        this.state={
            originalPassword:'',
            newPassword:'',
            repeatPassword:'',
        }
    }
    //输入原始密码
    inputOriginalPassword(e) {
        this.setState({
            originalPassword: e.target.value,
        });
    }
    //输入新密码
    inputNewPassword(e) {
        this.setState({
            newPassword: e.target.value,
        });
    }
    //输入重复新密码
    inputRepeatPassword(e) {
        this.setState({
            repeatPassword: e.target.value,
        });
    }
    handleConfirmClick(){
        const {  originalPassword, newPassword, repeatPassword } = this.state;

        if (!originalPassword) {
            message.error(`请填写原始密码`);
            return;
        }
        if (!newPassword) {
            message.error(`请填写新密码！`);
            return;
        }
        if (!repeatPassword) {
            message.error(`请填写重复新密码！`);
            return;
        }
        if (newPassword !== repeatPassword) {
            message.error(`两次密码不一致！`);
            return;
        }
        let url=allRouter.patientOrigin+'/doctor/changePassWord';
        $.ajax({
            url: url,
            type: "post",
            async: true,
            headers: { Authorization: 'Bearer '+sessionStorage.getItem('token')},
            data: {
                userPassword: this.state.originalPassword,
                newUserPassword: this.state.newPassword,
                // id:userInfo.id,
            },
            success: res => {
                // console.log('aaa', res)
                if (res.code === 200) {
                    this.setState({
                        originalPassword: '',
                        newPassword: '',
                        repeatPassword: '',
                    });
                    message.success('修改密码成功！即将跳转到登录页面',1,()=>{
                        sessionStorage.removeItem('UserInfo');
                        sessionStorage.removeItem('token');
                        browserHistory.push('/login');
                    });
                } else if(res.code === 201) {
                    message.error('原始密码错误');
                }else{
                    message.error('修改密码错误，请返回重新操作');
                }
            }
        });
    }
    handleCancelClick(){
        Modal.confirm({
            title:'取消保存',
            content:'确认取消保存并返回上一页？',
            onCancel:()=>{console.log('取消')},
            onOk:()=>{
                browserHistory.goBack();
            }
        })
    }
    render(){
        return (
            <div className="container" style={{fontSize:'18px'}}>
                <div className="show-template">
                    <span>*修改密码</span>
                </div>
                <Row style={{marginBottom:'40px'}}>
                    <Col span={2} >
                        原始密码:
                    </Col>
                    <Col span={20} >
                        <Input type="password" value={this.state.originalPassword} onChange={(e) => this.inputOriginalPassword(e)} placeholder="原始密码" />
                    </Col>
                </Row>
                <Row style={{marginBottom:'40px'}}>
                    <Col span={2}>
                        设置密码:
                    </Col>
                    <Col span={20}>
                        <Input type="password" value={this.state.newPassword} onChange={(e) => this.inputNewPassword(e)} placeholder="新密码" />
                    </Col>
                </Row>
                <Row>
                    <Col span={2}>
                        重复密码:
                    </Col>
                    <Col span={20}>
                        <Input type="password" value={this.state.repeatPassword} onChange={(e) => this.inputRepeatPassword(e)} placeholder="重复新密码" />
                    </Col>
                </Row>
                <Row style={{marginTop:'40px'}}>
                    <Col span={2} offset={9}><Button type='primary' onClick={this.handleConfirmClick.bind(this)}>确定</Button></Col>
                    <Col span={2} offset={3}><Button onClick={this.handleCancelClick.bind(this)}>取消</Button></Col>
                </Row>
            </div>
        );
    }
}
export default UpdatePassword;
